//
//  WmcPhotoTemplatesView.swift
//  YPWatermarkCamera
//
//  Created by Json on 2023/8/1.
//

import UIKit
import RxRelay

class WmcAlertTemplateGroupView: WmcLayoutAlertBaseGroupView {
    
    /// collectionView
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        let itemW = 86.0.wmc_autoHor
        let itemH = collcetionH - 1.0
        let spacing = 38.0.wmc_autoHor
        layout.itemSize = CGSize(width: itemW, height: itemH)
        layout.minimumLineSpacing = spacing
        let collectionV = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionV.backgroundColor = UIColor.clear
        return collectionV
    }()
    
    private let collcetionH = 137.wmc_autoHor
    /// 模板数据源
    private lazy var templates: [WmcPhotoTemplate] = {
        var tmps: [WmcPhotoTemplate] = []
        let `default` = WmcPhotoTemplate(title: "默认", style: .default)
        tmps.append(`default`)
        let workReport = WmcPhotoTemplate(title: "工作汇报", style: .workReport)
        tmps.append(workReport)
        return tmps
    }()
    
    /// 模板样式
    var style: WmcPhotoTemplate.Style = .default {
        didSet {
            let tmp = templates.first { $0.style == style }
            if let tmp = tmp {
                didSelected(template: tmp)
            }
        }
    }
    
    /// 当前选中的模板
    private var selectedTemlate: WmcPhotoTemplate?
    
    /// 选中模板回调
    let didSelectedTemplate: PublishRelay<WmcPhotoTemplate> = .init()
    
    // MARK: UI
    override func makeUI() {
        super.makeUI()
        titleLabel.text = "拼图模板"
        // collectionView
        stackView.addArrangedSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.height.equalTo(collcetionH)
        }
        collectionView.wmc_register(cellClasses: [WmcDefaultTemplateCell.self,
                                                  WmcWorkReportTemplateCell.self
                                                 ])
        collectionView.dataSource = self
        collectionView.delegate = self
    }

}

extension WmcAlertTemplateGroupView {
    
    /// 选中模板调用
    private func didSelected(template: WmcPhotoTemplate) {
        selectedTemlate?.isSelected = false
        template.isSelected = true
        selectedTemlate = template
        collectionView.reloadData()
    }
}

// MARK: UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
extension WmcAlertTemplateGroupView: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return templates.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let template = templates[indexPath.item]
        let cell: WmcBasePhotoTemplateCell
        switch template.style {
        case .default:
            cell = collectionView.wmc_dequeueReusableCell(cellClass: WmcDefaultTemplateCell.self, for: indexPath)
        case .workReport:
            cell = collectionView.wmc_dequeueReusableCell(cellClass: WmcWorkReportTemplateCell.self, for: indexPath)
        }
        cell.template = template
        return cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let template = templates[indexPath.item]
        didSelected(template: template)
        didSelectedTemplate.accept(template)
    }
}

